package com.example.dbcenter.conf;

import org.apache.log4j.Logger;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class ReflectUtil {
    private static Logger log = Logger.getLogger(ReflectUtil.class);

    public ReflectUtil() {
    }

    private static Object operate(Object obj, String fieldName, Object fieldVal, String type) {
        Object ret = null;

        try {
            Class<?> classType = obj.getClass();
            Field[] fields = classType.getDeclaredFields();

            for(int i = 0; i < fields.length; ++i) {
                Field field = fields[i];
                if (field.getName().equals(fieldName)) {
                    String firstLetter = fieldName.substring(0, 1).toUpperCase();
                    String getMethodName;
                    Method getMethod;
                    if ("set".equals(type)) {
                        getMethodName = "set" + firstLetter + fieldName.substring(1);
                        getMethod = classType.getMethod(getMethodName, field.getType());
                        ret = getMethod.invoke(obj, fieldVal);
                    }

                    if ("get".equals(type)) {
                        getMethodName = "get" + firstLetter + fieldName.substring(1);
                        getMethod = classType.getMethod(getMethodName);
                        ret = getMethod.invoke(obj);
                    }

                    return ret;
                }
            }
        } catch (Exception var12) {
            log.warn("reflect error:" + fieldName, var12);
        }

        return ret;
    }

    public static Object getVal(Object obj, String fieldName) {
        return operate(obj, fieldName, (Object)null, "get");
    }

    public static void setVal(Object obj, String fieldName, Object fieldVal) {
        operate(obj, fieldName, fieldVal, "set");
    }

    private static Method getDeclaredMethod(Object object, String methodName, Class<?>[] parameterTypes) {
        Class superClass = object.getClass();

        while(superClass != Object.class) {
            try {
                return superClass.getDeclaredMethod(methodName, parameterTypes);
            } catch (NoSuchMethodException var5) {
                superClass = superClass.getSuperclass();
            }
        }

        return null;
    }

    private static void makeAccessible(Field field) {
        if (!Modifier.isPublic(field.getModifiers())) {
            field.setAccessible(true);
        }

    }

    private static Field getDeclaredField(Object object, String filedName) {
        Class superClass = object.getClass();

        while(superClass != Object.class) {
            try {
                return superClass.getDeclaredField(filedName);
            } catch (NoSuchFieldException var4) {
                superClass = superClass.getSuperclass();
            }
        }

        return null;
    }

    public static Object invokeMethod(Object object, String methodName, Class<?>[] parameterTypes, Object[] parameters) throws InvocationTargetException {
        Method method = getDeclaredMethod(object, methodName, parameterTypes);
        if (method == null) {
            throw new IllegalArgumentException("Could not find method [" + methodName + "] on target [" + object + "]");
        } else {
            method.setAccessible(true);

            try {
                return method.invoke(object, parameters);
            } catch (IllegalAccessException var6) {
                return null;
            }
        }
    }

    public static void setFieldValue(Object object, String fieldName, Object value) {
        Field field = getDeclaredField(object, fieldName);
        if (field == null) {
            throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + object + "]");
        } else {
            makeAccessible(field);

            try {
                field.set(object, value);
            } catch (IllegalAccessException var5) {
                var5.printStackTrace();
            }

        }
    }

    public static Object getFieldValue(Object object, String fieldName) {
        Field field = getDeclaredField(object, fieldName);
        if (field == null) {
            throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + object + "]");
        } else {
            makeAccessible(field);
            Object result = null;

            try {
                result = field.get(object);
            } catch (IllegalAccessException var5) {
                var5.printStackTrace();
            }

            return result;
        }
    }
}